package com.aaa.system.service.imp;

import com.aaa.entity.DictData;
import com.aaa.entity.DictType;
import com.aaa.entity.User;
import com.aaa.system.mapper.DictDataMapper;
import com.aaa.system.mapper.DictTypeMapper;
import com.aaa.system.mapper.UserMapper;
import com.aaa.system.service.DictDataService;
import com.aaa.utils.TokenUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class DictDataServiceImp implements DictDataService {

    @Autowired
    private DictDataMapper dictDataMapper;

    @Autowired
    private DictTypeMapper dictTypeMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public Result<List<DictData>> findByType(String type) {
        try {
            String s = redisTemplate.opsForValue().get(type);
            if(StringUtils.isNotBlank(s)){
                List<DictData> dictDataRedis = JSON.parseArray(s, DictData.class);
                return new Result<>(200,"查询系统的状态",dictDataRedis);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        QueryWrapper<DictData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_type",type);
        List<DictData> dictData = dictDataMapper.selectList(queryWrapper);
        return new Result<>(200,"查询系统的状态",dictData);
    }

    @Override
    public Result<String> findDDByDictId(Long dictId) {
        //根据id查询字典类型
        QueryWrapper<DictType> wrapper = new QueryWrapper<>();
        if (Objects.nonNull(dictId)){
            wrapper.eq("dict_id",dictId);
        }
        DictType dictType = dictTypeMapper.selectOne(wrapper);
        return new Result<>(200,"查询成功",dictType.getDictType());
    }

    @Override
    public Result<IPage<DictData>> findAllTypeData(Long current, Long size, DictData dictData) {
        IPage<DictData> page = new Page<>(current,size);
        QueryWrapper<DictData> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(dictData.getDictType())){
            wrapper.eq("dict_type",dictData.getDictType());
        }
        if (StringUtils.isNotBlank(dictData.getDictLabel())){
            wrapper.like("dict_lable",dictData.getDictLabel());
        }
        if (Objects.nonNull(dictData.getStatus())){
            wrapper.eq("status",dictData.getStatus());
        }
        IPage page1 = dictDataMapper.selectPage(page, wrapper);
        return new Result<>(200,"查询成功",page1);
    }

    @Override
    public Result<Boolean> saveOrDictData(DictData dictData) {
        String token = WebUtil.getRequest().getHeader("token");
        String username = TokenUtils.getInfo(token).get("username").toString();
        //根据手机号进行查询
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",username);
        queryWrapper.eq("status",0);
        queryWrapper.eq("del_flag",0);
        User userdata = userMapper.selectOne(queryWrapper);
        //id 修改
        if (Objects.nonNull(dictData.getDictCode())){
            dictData.setUpdateTime(LocalDateTime.now());
            dictData.setCreateBy(userdata.getUserName());
            int i = dictDataMapper.updateById(dictData);
            return new Result<>(200,"修改成功",i>0?true:false);
        }
        dictData.setCreateTime(LocalDateTime.now());
        dictData.setCreateBy(userdata.getUserName());
        int i = dictDataMapper.insert(dictData);
        return new Result<>(200,"添加成功",i>0?true:false);
    }

    @Override
    public Result<Boolean> delDictData(Long[] dictCodes) {
        int i = dictDataMapper.bachDictData(dictCodes);
        return new Result<>(200,"删除成功",i>0?true:false);
    }
}
